package com.lanju.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lanju.dao.AreaCommunityDao;
import com.lanju.dao.HouseDao;
import com.lanju.dao.HouseHoldDao;
import com.lanju.dao.SystemAdminDao;
import com.lanju.dao.UserDao;
import com.lanju.entity.Community;
import com.lanju.entity.CommunityImg;
import com.lanju.entity.House;
import com.lanju.entity.HouseEquipment;
import com.lanju.entity.HouseImg;
import com.lanju.entity.HousePrice;
import com.lanju.entity.HouseZuKeInfo;
import com.lanju.entity.User;
import com.lanju.model.HouseAndImgModel;
import com.lanju.service.HouseService;

@Service
public class HouseServiceImpl implements HouseService {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(HouseServiceImpl.class);

	@Autowired
	HouseDao houseDao;

	@Autowired
	SystemAdminDao systemAdminDao;

	@Autowired
	AreaCommunityDao areaCommunityDao;

	@Autowired
	HouseHoldDao houseHoldDao;
	
	@Autowired
	UserDao userDao;

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(value = "financialmarketingTM", readOnly = true)
	public Map<String, Object> queryHouseList(Map<String, Object> param) {
		System.out.println("" + param);
		List<House> list = houseDao.queryHouseList1(param);
		List<HouseAndImgModel> resultList = new ArrayList<>();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				HouseAndImgModel model = new HouseAndImgModel();
				model.setHouse(list.get(i));
				System.out.println(list.get(i).getHouseId().toString());
				List<HouseImg> hi = houseDao.getHouseIMgList(list.get(i)
						.getHouseId().toString(), "");
				model.setImglist(hi);
				resultList.add(model);
			}
		}
		long count = houseDao.countHouseList1(param);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", resultList);
		return map;
	}

	@SuppressWarnings("unused")
	@Override
	@Transactional(value = "financialmarketingTM", readOnly = true)
	public Map<String, Object> queryAllFlag(Map<String, Object> param) {
		List<Map<String, Object>> list = houseDao.queryAllFlag(param);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("rows", list);
		return map;
	}

	/**
	 * 将List<String>集合 转化为String 如{"aaa","bbb"} To 'aaa','bbb'
	 */
	public static String convertListToString(List<String> strlist) {
		StringBuffer sb = new StringBuffer();
		if (strlist != null && strlist.size() > 0) {
			for (int i = 0; i < strlist.size(); i++) {
				if (i == 0) {
					sb.append("'").append(strlist.get(i)).append("'");
				} else {
					sb.append(",").append("'").append(strlist.get(i))
							.append("'");
				}
			}
		}
		return sb.toString();
	}

	public static String convertListMapToString(List<Map> strlist, String id) {
		StringBuffer sb = new StringBuffer();
		if (strlist != null && strlist.size() > 0) {
			for (int i = 0; i < strlist.size(); i++) {
				if (i == 0) {
					sb.append("'").append(strlist.get(i).get("" + id + ""))
							.append("'");
				} else {
					sb.append(",").append("'")
							.append(strlist.get(i).get("" + id + ""))
							.append("'");
				}
			}
		}
		return sb.toString();
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(value = "financialmarketingTM", readOnly = true)
	public Map<String, Object> queryHomeDetailByHouseId(
			Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isCollect=false;
		try {
			House house = houseDao.queryHomeDetailByHouseId1(param);
			Set<HouseEquipment> eset = house.getHouseEquipmentSet();
			List<Map<String, Object>> list1 = houseDao.queryHomeLocationByHouseId(param);
			List<Map<String, Object>> list2 = houseDao.queryHomeImgByHouseId(param);
			List<Map<String, Object>> list3 = houseDao.queryHomeFlagByHouseId(param);
			if(param.get("userId") !=null){
				isCollect = houseDao.queryCollectByHouseId(param);	
			}
			if (house != null) {
				Community c = areaCommunityDao.getAreaCommunityDetail(house.getCommunity_id());
				Set<CommunityImg> set = c.getCommunityimg();
				User u = userDao.getById(house.getHoldId());
				map.put("household", u);
				map.put("community", c);
				map.put("c_img_list", set);
			}
			map.put("houseDetail", house);
			map.put("eset", eset);
			map.put("houseLocation", list1);
			map.put("houseImg", list2);
			map.put("houseFlag", list3);
			map.put("houseCollect", isCollect);
			map.put("result", true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("result", false);
		}
		return map;
	}

	@Override
	public Map<String, Object> getById(Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		House house = houseDao.queryHomeDetailByHouseId1(param);
		map.put("rows", house);
		return map;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(value = "financialmarketingTM", rollbackFor = Exception.class)
	public void save(House house) {
		houseDao.save(house);
	}

	@Override
	@Transactional(value = "financialmarketingTM", readOnly = true)
	public Map<String, Object> queryMyHouseList(Map<String, Object> param) {

		List<HouseAndImgModel> resultList = new ArrayList<>();
		Map<String, Object> map = new HashMap<String, Object>();

		List<Map> apList = houseDao.queryAPList(param);

		if (apList.size() > 0) {
			String apStr = convertListMapToString(apList, "houseId");
			param.put("apStr", apStr);
			List<House> list = houseDao.queryMyHouseList(param);
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					HouseAndImgModel model = new HouseAndImgModel();
					model.setHouse(list.get(i));
					System.out.println(list.get(i).getHouseId().toString());
					List<HouseImg> hi = houseDao.getHouseIMgList(list.get(i).getHouseId().toString(), "1");
					model.setImglist(hi);
					resultList.add(model);
				}
			}
			long count = houseDao.countMyHouseList(param);
			map.put("total", count);
		}

		map.put("rows", resultList);
		return map;
	}

	@Override
	@Transactional(value = "financialmarketingTM", rollbackFor = Exception.class)
	public Map<String, Object> addHousePrice(HousePrice hp) {
		Map<String, Object> map = new HashMap<>();
		try {
			houseDao.addHousePrice(hp);
			map.put("result", true);
		} catch (Exception e) {
			map.put("result", false);
			e.printStackTrace();
		}
		return map;
	}

	@Override
	public Map<String, Object> getAllHouseList(Map<String, Object> param) {
		Map<String, Object> map = null;
		try {
			User user=userDao.getById(param.get("id").toString());
			map = new HashMap<>();
			map.put("list", user.getHouseSet());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	@Override
	public HouseZuKeInfo getHouseZuKeInfo(Map<String, Object> map) {
		return houseDao.getHouseZuKeInfo(map);
	}
}
